<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Futures\Controller;
use Think\Controller;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends BaseController {
	// 猜涨跌首页
	public function index() {
	$list = M('futures_option')->where('status = 1')->select();
	$this->assign('list',$list);
        $this->display('welcome_index');
	exit;	
        $user_auth= cookie('user_auth');
        $user_id=$user_auth['uid'];
        $openid= $user_auth['openid'];
        //获取账户余额
        $user_money = M('Follow')->where(array('openid'=>$user_auth['openid']))->getField('money');
        $this->assign('money',$user_money);
        //获取当前期购买开始时间和结束时间
        $time=time();
        //$time=1456216200;
        // $date=date('Ymd',$time);
        // $is_work=is_rest($date);//是否是在工作日；
        // foreach($is_work as $key=>$val){
        //     $is_work=intval($val);
        // }
        
        // if($is_work!=0){
        //     $arr['status']=false;
        //     $arr['msg']="节假日休市";
        //     //$this->ajaxReturn($arr,'JSON');
        // }
        //下单时段为交易日：9:00:00-11:30 13:30-15:00 21:00-次日凌晨1:00
        $period_one_start = mktime(9,00,00,date('m',$time),date('d',$time),date('Y',$time));
        $period_one_end = mktime(11,30,00,date('m',$time),date('d',$time),date('Y',$time));

        $period_two_start = mktime(13,30,00,date('m',$time),date('d',$time),date('Y',$time));
        $period_two_end = mktime(15,00,00,date('m',$time),date('d',$time),date('Y',$time));

        $period_three_start= mktime(21,00,00,date('m',$time),date('d',$time),date('Y',$time));
        //$period_three_time= mktime(1,00,00,date('m',strtotime('+1 day')),date('d',strtotime('+1 day')),date('Y',strtotime('+1 day')));
        $period_three_end= mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
        //是工作日并且要在这三个时间段，不是工作日，时间在1点之前，并且前一天是工作日
        $last_date = date('Ymd',strtotime('-1 day'));//前一天
        $is_last_work=is_rest($last_date);//是否是在工作日；
        $is_last_work=$is_last_work[$last_date];
        $cur_night_end=mktime(1,00,00,date('m'),date('d'),date('Y'));
        $cur_night_start=mktime(0,00,00,date('m'),date('d'),date('Y'));
        $is_period_one = (($period_one_start<time())&&(time()<$period_one_end))?true:false;
        $is_period_two = (($period_two_start<time())&&(time()<$period_two_end))?true:false;
        $is_period_three = (($period_three_start<time())&&(time()<$period_three_end))?true:false;

        // if(!(($is_last_work==0&&time()<$cur_night_end&&time()>$cur_night_start)||($is_work==0 && ($is_period_one||$is_period_two||$is_period_three)))){
        //     $arr['status']=false;
        //     $arr['msg']="下单时段为交易日:9:00-11:30 13:30-15:00 21:00-次日凌晨01:00";
        //     //$this->ajaxReturn($arr,'JSON');
        // }

        

        $start_time= mktime(9,00,00,date('m',$time),date('d',$time),date('Y',$time));
        $end_time= mktime(15,00,00,date('m',$time),date('d',$time),date('Y',$time));
        /*
        if(($time>$end_time || $time<$start_time)){//是工作日，加时间判断
        }else{
            $arr['status']=false;
            $arr['msg']="购买时间在15:30之后 或 8:30之前";
            $this->ajaxReturn($arr,'JSON');
        }*/
        //获取开始时间和结束时间
        // if($time<$start_time){
        //  $time_end_result=strtotime(get_time($time,0));  
        // }elseif($time>$end_time){
        //  $time_end_result=strtotime(get_time($time,1));
        // }else{
        //  $time_end_result=strtotime(get_time($time,1));
        // }
        // if($is_period_three || (time()<$cur_night_end&&time()>$cur_night_start)){
        //     $time_end_result=strtotime(get_time($time,2));
        // }else{
        //     $time_end_result=strtotime(get_time($time,1));
        // }
        
        if($is_period_three){
            //21-24结束时间是第二个交易日上午11点半
            $time_end_result=strtotime(get_time($time,1));
            $end_time_value= mktime(11,30,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
        }elseif(time()<$cur_night_end&&time()>$cur_night_start){
            //结束时间是交易日上午11点半
            $time_end_result=strtotime(get_time($time,0));
            $end_time_value= mktime(11,30,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
        }elseif($is_period_one){    //9-11:30
            $end_time_value= mktime(15,00,00,date('m'),date('d'),date('Y'));        
        }elseif($is_period_two){    //13:00-15:00
            $end_time_value= mktime(1,00,00,date('m',strtotime('+1 day')),date('d',strtotime('+1 day')),strtotime('+1 day'));
        }
        
        //$end_time_value= mktime(15,00,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
        $start_time_value = time();
        //$start_time_value=mktime(9,00,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
		// $time=time();
		// //$time= mktime(7,30,00,date('m',$time),date('d',$time)+4,date('Y',$time));
		// $start_time= mktime(9,00,00,date('m',$time),date('d',$time),date('Y',$time));
		// $end_time= mktime(15,00,00,date('m',$time),date('d',$time),date('Y',$time));

		// if($time<$start_time){
		// 	$time_end_result=strtotime(get_time($time,0));	
		// }else if($time>$end_time){
		// 	$time_end_result=strtotime(get_time($time,1));
		// }else{//默认
		// 	$time_end_result=strtotime(get_time($time,1));
		// }
		// $end_time_value= mktime(15,00,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
		// $start_time_value=mktime(9,00,00,date('m',$time_end_result),date('d',$time_end_result),date('Y',$time_end_result));
        //获取所有进行中合约
        $futures_where = array();
        $futures_where['status'] = 1;
        $futures_where['end_time'] = array('gt',time());
        $futures = M('Futures')->where($futures_where)->select();
        foreach ($futures as $key => $future) {
        	//$futures[$key]['begin_time']=date("m月d日",$start_time_value);
            $futures[$key]['begin_time']=date("m月d日 H:i",$start_time_value);
            if($end_time_value){
                $futures[$key]['end_time']=date("m月d日 H:i",$end_time_value);
            }else{
                $futures[$key]['end_time']='不在交易时段';
            }
            
            $future_data = get_futures_data($future['code'],$future['type_sign']);
            if(false!==$future_data){
                if($future['type_sign']=='commodity'){
                    $futures[$key]['last_price'] = $future_data[8]; //最新价
                    $futures[$key]['sell_price'] = $future_data[7]; //卖价
                    $futures[$key]['buy_price']  = $future_data[6]; //买价
                }elseif($future['type_sign']=='stock'){
                    //dump($future_data);
                    $futures[$key]['last_price'] = $future_data[3];
                    $futures[$key]['sell_price'] = $future_data[3]; //卖价
                    $futures[$key]['buy_price']  = $future_data[3]; //买价
                }
                
            }else{    
                unset($futures[$key]);
            }
        }
        //首页订单进行中3条数据
        $where_order['user_id']=array('eq',$user_id);
        $where_order['a.status']=array('lt',2);
        //$where_order['authority_status']=array('eq','0');
        //$list=M('futures_order')->table('wp_futures_order as a')->where($where_order)->order('a.begin_time desc')->limit('0,3')->select();
        $list=M('futures_order')->table('wp_futures_order as a')->where($where_order)->order('a.begin_time desc')->select();
        //print_r($list);exit;
     	foreach($list as $k=>$o){
            
            $list[$k]['create_time']=date("Ymd",$o['begin_time']);//购买时间
            $future_three = M('Futures')->field('name,code,type_sign,stop_loss,premium')->find($o['futures_id']);
            //获取实时数据写入收盘后最新价
		    $future_data = get_futures_data($future_three['code'],$future_three['type_sign']); //从接口获取数据
            //print_r($future_three);exit;
            if(false!==$future_data){//当前价
                if($future_three['type_sign']=='commodity'){
                    $closePrice = $future_data[8];
                }elseif($future_three['type_sign']=='stock'){
                     $closePrice = $future_data[3];
                }
            }
            //print_r($closePrice);exit;
            if($o['direction']==1){
                //计算收益公式：盈利=（最新价-开盘价）*手数*权利金 (买涨)
                //$profit = ($closePrice-$o['open_price'])*$o['number']*$o['premium'];
                $profit = ($closePrice-$o['open_price']);
                //print_r($profit);exit;
            }elseif($o['direction']==0){
                //计算收益公式：盈利=（最新价-开盘价）*手数*权利金 (买跌)
                //$profit = ($o['open_price']-$closePrice)*$o['number']*$o['premium'];
                $profit = ($o['open_price']-$closePrice);
            }elseif($o['direction']==2){
                //看平分为涨和跌两个方向
                $ping_d =  $o['open_price']-$closePrice;
                //如果卖出价大于买入价为涨，计算方式为:买入价*1.01-卖出价
                ////如果卖出价和买入价相同，计算方式为:买入价*0.01
                if($ping_d<=0){ //小于0为涨
                    //$profit=$o['open_price']*1.01-$closePrice;
                    $profit=$closePrice-$o['open_price'];
                }else{ //大于0为跌
                    //如果卖出价小于买入价为跌，计算方式为:卖出价-买入价0.99
                    //$profit=$closePrice-$o['open_price']*0.99;
                    $profit=$o['open_price']-$closePrice;
                }
                $profit=$future_three['premium']*2-$profit;
                if($profit>0){  //在看平范围以内
                    if($ping_d<=0){
                        $closePrice=$o['open_price']+$future_three['premium']*2;
                    }else{
                        $closePrice=$o['open_price']-$future_three['premium']*2;
                    }
                }    
            }
            //最大值为权益金的三倍，看平的最大值是权益金的两倍TODO
            if($o['direction']==1){
                //盈利最大值
                $max_profit = $future_three['premium']*3;
                if($profit>$max_profit){
                    $profit = $max_profit;
                }
            }elseif($o['direction']==0){
                $max_profit = $future_three['premium']*3;
                if($profit>$max_profit){
                    $profit = $max_profit;
                }
            }
            $gain = $profit*($o['number']);
            if($profit<0) $profit=0.00;
            if($gain<0) $gain=0.00;
            $id = $o['id'];
            $arr[$id]['id'] = $o['id'];
            $arr[$id]['direction'] = $o['direction'];
            $arr[$id]['name'] = $future_three['name'];           
            $arr[$id]['gain'] = $gain;
            $arr[$id]['close'] =$closePrice;
            $arr[$id]['number'] = $o['number'];
            $arr[$id]['open_price'] = $o['open_price'];
            $arr[$id]['status'] = $o['status'];
            $arr[$id]['end_time'] = $o['end_time'];
		 }

        $this->assign('futures',$futures);
        $this->assign('arr',$arr);

        //print_r($arr);exit;
        //文章列表和详情页
        $document = M('document');
        $new = $document->where('category_id=46')->limit(5)->order('id desc')->select();
        $this->assign('new',$new);
        //有多少张代金劵
        $discount = M('futures_voucher_records');
        //查询所有可用代金卷
        $voucher_where['user_id'] = $user_id;
        $voucher_where['count'] = array('gt',0);   
        $voucher_where['overdue_time'] = array('gt',time());
        $vouchers = $discount->where($voucher_where)->select();// 查询满足要求的总记录数
        //dump($vouchers);
        $count = count($vouchers);
        $this->assign('vouchers',$vouchers);
        $this->assign('count',$count);
        
        $this->display();
	}

}    
